import React, { PureComponent } from 'react';
import accounting from 'accounting';
import { NoDataC, CircleChartsC } from '../../../../../comps';

export default class ChartsPie extends PureComponent {
  render() {
    const { data = [], height, colorType } = this.props;
    return data && data.length ? (
      <div style={{ height: `${height}`, width: '100%', display: 'inline-block' }}>
        <CircleChartsC
          data={data}
          height={height}
          type='ring'
          center={['35%', '50%']}
          legendX={'65%'}
          legendTextWidth={180} //图例文本显示宽度
          colorType={colorType}
          legendFormatter={(name) => {
            // 获取legend显示内容
            let total = 0;
            let tarValue = 0;
            let holdAmount = 0;
            for (let i = 0, l = data.length; i < l; i++) {
              total += data[i].value * 1;
              if (data[i].name == name) {
                tarValue = data[i].value * 1;
                // holdAmount = data[i].holdAmount * 1;
              }
            }
            let p = total ? ((tarValue / total) * 100).toFixed(2) : '0.00';
            // let mValue = accounting.formatNumber(Number(holdAmount), 2);
            return `{a|${name.length > 8 ? `${name.slice(0, 6)}... ${p}%` : `${name} ${p}%`}}`;
          }}
        />
      </div>
    ) : (
      <NoDataC />
    );
  }
}
